package com.zkyc.framework.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.Properties;

public class ExecUtil {

    /**
     * 执行命令
     **/
    public static String exec(String command) {
        Process process = null;
        String cmd = getOsCmd() + command;
        try {
            process = Runtime.getRuntime().exec(cmd);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(new String(line.getBytes(), "GBK"));
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            assert process != null;
            process.destroy();
        }
    }


    /**
     * 获取当前操作系统
     **/
    public static String getOsCmd() {
        Properties props = System.getProperties(); //获得系统属性集
        String osName = props.getProperty("os.name"); //操作系统名称
        if (osName.toLowerCase().contains("linux")) {
            return "/bin/sh -c";
        } else if (osName.toLowerCase().contains("windows")) {
            return "cmd /c";
        } else {
            throw new RuntimeException("服务器不是linux|windows操作系统");
        }
    }

    /**
     * 获取本机内网ip
     * @return
     */
    public static String getIp() throws Exception {
        String ip = InetAddress.getLocalHost().getHostAddress();
        return ip;
    }


}
